home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / Hello / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  7.0 KB  |  244 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "Hello.hpp"
  11.  
  12. #ifndef CONTENT_H
  13. #include "Content.h"
  14. #endif
  15.  
  16. #ifndef PART_H
  17. #include "Part.h"
  18. #endif
  19.  
  20. // ----- Framework Includes -----
  21.  
  22. #ifndef FWKIND_H
  23. #include "FWKind.h"
  24. #endif
  25.  
  26. #ifndef FWRESOUR_H
  27. #include "FWResour.h"
  28. #endif
  29.  
  30. #ifndef FWSUSINK_H
  31. #include "FWSUSink.h"
  32. #endif
  33.  
  34. #ifndef FWSUUTIL_H
  35. #include "FWSUUtil.h"
  36. #endif
  37.  
  38. #ifndef FWMEMHLP_H
  39. #include "FWMemHlp.h"
  40. #endif
  41.  
  42. #ifndef FWBUFSIN_H
  43. #include "FWBufSin.h"
  44. #endif
  45.  
  46. #ifndef SOM_ODStorageUnit_xh
  47. #include <StorageU.xh>
  48. #endif
  49.  
  50. //========================================================================================
  51. //    Runtime info
  52. //========================================================================================
  53.  
  54. #ifdef FW_BUILD_MAC
  55. #pragma segment odfhello
  56. #endif
  57.  
  58. FW_DEFINE_AUTO(CHelloContent)
  59.  
  60. //========================================================================================
  61. //    CHelloContent class
  62. //========================================================================================
  63.  
  64. //----------------------------------------------------------------------------------------
  65. // CHelloContent constructor
  66. //----------------------------------------------------------------------------------------
  67.  
  68. CHelloContent::CHelloContent(Environment* ev, CHelloPart* part) :
  69.     FW_CContent(ev, part),
  70.     fTextData(""),
  71.     fCentered(FALSE),
  72.     fHelloPart(part)
  73. {
  74.     // ----- Initialize the text data strings -----
  75.     FW_PSharedLibraryResourceFile resFile(ev);
  76.  
  77.     FW_CString platformString;
  78.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, FW_kMultiStringRes, kFirstString, fTextData);
  79.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, FW_kMultiStringRes, kPlatformString, platformString);
  80.     fTextData += platformString;
  81. }
  82.  
  83. //----------------------------------------------------------------------------------------
  84. // CHelloContent destructor
  85. //----------------------------------------------------------------------------------------
  86.  
  87. CHelloContent::~CHelloContent()
  88. {
  89. }
  90.  
  91. //----------------------------------------------------------------------------------------
  92. // CHelloContent::GetTextData
  93. //----------------------------------------------------------------------------------------
  94.  
  95. const FW_CString& CHelloContent::GetTextData()
  96. {
  97.     return fTextData;
  98. }
  99.  
  100. //----------------------------------------------------------------------------------------
  101. // CHelloContent::SetTextData
  102. //----------------------------------------------------------------------------------------
  103.  
  104. void CHelloContent::SetTextData(Environment* ev, const FW_CString& newText)
  105. {
  106.     fTextData = newText;
  107.     fHelloPart->PartChanged(ev, kDontMarkDraft, kDontMarkFrame);
  108.     // Since SetTextData is always called by a command, we don't need to
  109.     // mark the draft or the frame because the framework takes care of it.  
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. // CHelloContent::ClearTextData
  114. //----------------------------------------------------------------------------------------
  115.  
  116. void CHelloContent::ClearTextData(Environment* ev)
  117. {
  118.     FW_CString newString;
  119.     FW_PSharedLibraryResourceFile resFile(ev);
  120.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, FW_kMultiStringRes, kBlankString, newString);
  121.  
  122.     fTextData = newString;
  123.     fHelloPart->PartChanged(ev, kDontMarkDraft, kDontMarkFrame);
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // CHelloContent::ExternalizeKind
  128. //----------------------------------------------------------------------------------------
  129.  
  130. void CHelloContent::ExternalizeKind(Environment* ev,
  131.                                     ODStorageUnit* storageUnit,
  132.                                     FW_CKind* kind,
  133.                                     FW_StorageKinds storageKind,
  134.                                     FW_CPromise* promise,
  135.                                     FW_CCloneInfo* cloneInfo)
  136. {
  137.     FW_UNUSED(cloneInfo);
  138.     FW_UNUSED(storageKind);
  139.     FW_UNUSED(promise);
  140.     
  141.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  142.     FW_CWritableStream stream(suSink);
  143.  
  144.     if (kind->IsPartKind(ev)) 
  145.     {
  146.         stream << fCentered;
  147.         stream << fTextData;
  148.     }
  149.     else if (kind->IsEqual(ev, 'TEXT', kODPlatformDataType))
  150.     {
  151.         stream.Write(fTextData.RevealBuffer(), fTextData.GetByteLength());
  152.     }
  153.     else if (kind->IsEqual(ev, 'TEXT', kODPlatformFileType))
  154.     {
  155.         FW_DEBUG_MESSAGE("Should never externalize TEXT file");
  156.     }
  157.     
  158.     // ----- Clear the end of the focused value -----
  159.     FW_SUDeleteEndOfFocusedValue(ev, storageUnit);
  160. }
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // ReadStringFromStream
  164. //----------------------------------------------------------------------------------------
  165.  
  166. static void ReadStringFromStream(Environment* ev, FW_CString& string, FW_CReadableStream& stream, unsigned long textSize)
  167. {
  168. FW_UNUSED(ev);
  169.     FW_CAcquireTemporaryMemory tempMemory(textSize + 1);
  170.     char* buffer = (char*)tempMemory.GetPointer();
  171.  
  172.     stream.Read(buffer, textSize);
  173.  
  174.     buffer[textSize] = 0;
  175.     string = "";
  176.     string.Append((const FW_Char*)buffer, textSize);
  177. }
  178.  
  179. //----------------------------------------------------------------------------------------
  180. // CHelloContent::InternalizeKind
  181. //----------------------------------------------------------------------------------------
  182.  
  183. FW_Boolean CHelloContent::InternalizeKind(Environment* ev,
  184.                                      ODStorageUnit* sourceSU, 
  185.                                       FW_CKind* kind,
  186.                                      FW_StorageKinds storageKind,
  187.                                      FW_CCloneInfo* cloneInfo)
  188. {
  189.     FW_UNUSED(cloneInfo);
  190.     
  191.     unsigned long size = sourceSU->GetSize(ev);
  192.     
  193.     if (kind->IsPartKind(ev) || kind->IsEqual(ev, 'TEXT', kODPlatformDataType))
  194.     {
  195.         FW_PStorageUnitSink suSink(ev, sourceSU, kODPropContents, kind->GetType(ev));
  196.         FW_PBufferedSink sink(ev, suSink);
  197.         FW_CReadableStream stream(sink);
  198.         
  199.         if (kind->IsPartKind(ev))
  200.         {
  201.             stream >> fCentered;
  202.             stream >> fTextData;
  203.         }
  204.         else            
  205.             ReadStringFromStream(ev, fTextData, stream, sink->GetLength(ev) /*size*/);
  206.     }
  207.     else if (kind->IsEqual(ev, 'TEXT', kODPlatformFileType))
  208.     {
  209.         FW_PFileValueSink sink(ev, sourceSU);
  210.         FW_CReadableStream stream(sink);
  211.         ReadStringFromStream(ev, fTextData, stream, sink->GetLength(ev));
  212.     }
  213.     else
  214.     {
  215.         FW_DEBUG_MESSAGE("CHelloContent::InternalizeKind - Unknown type");
  216.     }
  217.         
  218.     if (storageKind != FW_kPartStorage)
  219.     {
  220.         // Invalidate the presentation, so that the display frames
  221.         // are redrawn after a data transfer.
  222.         fHelloPart->PartChanged(ev, kDontMarkDraft, kDontMarkFrame);
  223.     }
  224.  
  225.     return TRUE;
  226. }
  227.  
  228. //----------------------------------------------------------------------------------------
  229. // CHelloContent::CenterText
  230. //----------------------------------------------------------------------------------------
  231.  
  232. void CHelloContent::CenterText(Environment* ev, FW_Boolean state)
  233. {
  234.     if (state != fCentered)
  235.     {
  236.         fCentered = state;
  237.  
  238.         // CenterText is called directly by DoMenu, so we must make sure that
  239.         // the draft and the frame are marked as changed.  
  240.         fHelloPart->PartChanged(ev, kMarkDraft, kMarkFrame);
  241.     }
  242. }
  243.  
  244.